package com.sinry.gateway.controller;

import cn.hutool.http.HttpUtil;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author linxinlei
 * @project sinry-cloud
 * @description 服务接口
 * @date 2022/5/16
 **/
@RestController
@RequestMapping("/service")
public class ServiceController {

    private final DiscoveryClient discoveryClient;

    public ServiceController(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }

    @GetMapping("getAll")
    public Mono<List<String>> getServiceList() {
        List<String> services = this.discoveryClient.getServices();

        return Mono.just(services);
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 2000; i++) {
            CompletableFuture.runAsync(() -> {
                System.out.println(HttpUtil.post("http://localhost:8999/sinry-user-api/revert", ""));
            }, executorService);
        }
    }
}
